#ifndef _EDITOR_C
#define _EDITOR_C

#include "editor.h"

//Draw the menu and other functions specific to the editor
void draw_menu(void){
     rect(menu_buffer,1,1,250,15,makecol(255,255,255));
     rect(menu_buffer,1,16,34,49,makecol(255,255,255));
     rect(menu_buffer,1,16,250,78,makecol(255,255,255));
     rect(menu_buffer,508,32,639,54,makecol(255,255,255));
     rect(menu_buffer,508,56,639,78,makecol(255,255,255));
     rect(menu_buffer,474,0,640,31,makecol(255,255,255));
     rect(menu_buffer,540,0,573,31,makecol(rand()%255,rand()%255,rand()%255));
     textprintf_ex(menu_buffer,font,510,35,makecol(255,255,255),-1,"x1: %d | y1:%d",sx1,sy1);
     textprintf_ex(menu_buffer,font,510,45,makecol(255,255,255),-1,"x2: %d | y2:%d",sx2,sy2);
     textprintf_ex(menu_buffer,font,510,58,makecol(255,255,255),-1,"world_x: %d",world_x);
     textprintf_ex(menu_buffer,font,510,68,makecol(255,255,255),-1,"world_y: %d",world_y);
     textprintf_ex(menu_buffer,font,20,4,makecol(255,255,255),-1,"ACTIVE TILE ATTRIBUTES");
     textprintf_ex(menu_buffer,font,45,23,makecol(255,255,255),-1,"X1: %d | X2: %d",tiles[ty][tx].x1,tiles[ty][tx].x2);
     textprintf_ex(menu_buffer,font,45,33,makecol(255,255,255),-1,"Y1: %d | Y2: %d",tiles[ty][tx].y1,tiles[ty][tx].y2);
     textprintf_ex(menu_buffer,font,45,43,makecol(255,255,255),-1,"Attribute: %d  (PRESS A)",tiles[ty][tx].att);
     textprintf_ex(menu_buffer,font,45,53,makecol(255,255,255),-1,"Moveable:  %d  (PRESS M)",tiles[ty][tx].mov);
     textprintf_ex(menu_buffer,font,45,63,makecol(255,255,255),-1,"Energy:    %d  (PRESS E)",tiles[ty][tx].energy);
     textprintf_ex(menu_buffer,font,260,1,makecol(255,255,255),-1,"ESC-EXIT PROGRAM");
     textprintf_ex(menu_buffer,font,260,11,makecol(255,255,255),-1,"1/2-CURRENT TILE");
     textprintf_ex(menu_buffer,font,260,21,makecol(255,255,255),-1,"3/4-SELECT TILE");
     textprintf_ex(menu_buffer,font,260,31,makecol(255,255,255),-1,"D-DRAW SELECTED");
     textprintf_ex(menu_buffer,font,260,41,makecol(255,255,255),-1,"F-FLOOD-FILL SELECTED");
     textprintf_ex(menu_buffer,font,260,51,makecol(255,255,255),-1,"DEL-CLEAR ALL");
     textprintf_ex(menu_buffer,font,260,61,makecol(255,255,255),-1,"S-SAVE MAP");
     textprintf_ex(menu_buffer,font,260,71,makecol(255,255,255),-1,"L-LOAD MAP");
     textprintf_right_ex(menu_buffer,font,506,71,makecol(0,255,0),-1,"['%s']",cur_map_name);
     textprintf_ex(menu_buffer,font,2,52,makecol(255,255,255),-1,"TILE#");
     textprintf_ex(menu_buffer,font,2,60,makecol(255,255,255),-1,"%d",tiles[ty][tx].bmp);
     stretch_blit(tile[tiles[ty][tx].bmp],menu_buffer,0,0,TILE_SIZE,TILE_SIZE,2,17,32,32);
     stretch_blit(tile[tdraw1],menu_buffer,0,0,TILE_SIZE,TILE_SIZE,475,1,32,30);
     stretch_blit(tile[tdraw2],menu_buffer,0,0,TILE_SIZE,TILE_SIZE,508,1,32,30);
     stretch_blit(tile[tdraw3],menu_buffer,0,0,TILE_SIZE,TILE_SIZE,541,1,32,30);
     stretch_blit(tile[tdraw4],menu_buffer,0,0,TILE_SIZE,TILE_SIZE,574,1,32,30);
     stretch_blit(tile[tdraw5],menu_buffer,0,0,TILE_SIZE,TILE_SIZE,607,1,32,30);    
}    

void do_logic(void){
     if(sx1>MAP_W*TILE_SIZE-TILE_SIZE)sx1=MAP_W*TILE_SIZE-TILE_SIZE;
     if(sx2>MAP_W*TILE_SIZE)sx2=MAP_W*TILE_SIZE;
     if(sy1>MAP_H*TILE_SIZE-TILE_SIZE)sy1=MAP_H*TILE_SIZE-TILE_SIZE;
     if(sy2>MAP_H*TILE_SIZE)sy2=MAP_H*TILE_SIZE;
     if(sx1<0)sx1=0;
     if(sx2<TILE_SIZE)sx2=TILE_SIZE;
     if(sy1<0)sy1=0;
     if(sy2<TILE_SIZE)sy2=TILE_SIZE;
     if(map_x/TILE_SIZE<cam_x)move_camera(LEFT);
     if(map_x/TILE_SIZE>cam_x)move_camera(RIGHT);
     if(map_y/TILE_SIZE<cam_y)move_camera(UP);
     if(map_y/TILE_SIZE>cam_y)move_camera(DOWN);
     if(world_x<0)world_x=0;
     if(world_y<0)world_y=0;
     if(world_x>MAP_W*TILE_SIZE-TILE_SIZE)world_x=MAP_W*TILE_SIZE-TILE_SIZE;
     if(world_y>MAP_H*TILE_SIZE-TILE_SIZE)world_y=MAP_H*TILE_SIZE-TILE_SIZE;
     if(map_x<0)map_x=0;
     if(map_y<0)map_y=0;
     if(map_x>MAP_W*TILE_SIZE-EDIT_MAP_SIZE_W)map_x=MAP_W*TILE_SIZE-EDIT_MAP_SIZE_W;
     if(map_y>MAP_H*TILE_SIZE-EDIT_MAP_SIZE_H)map_y=MAP_H*TILE_SIZE-EDIT_MAP_SIZE_H;
     if(tx>MAP_W-1)tx=MAP_W-1;
     if(tx<0)tx=0;
     if(ty>MAP_H-1)ty=MAP_H-1;
     if(ty<0)ty=0;
     tpic=map[ty][tx];
    
}    

void check_input(void){
    int i,j;  
    if(key[KEY_ESC]&&!keyhold[KEY_ESC]){
        keyhold[KEY_ESC]=1;
        quit=1;
    }    
    else {
        if(!key[KEY_ESC])keyhold[KEY_ESC]=0;
    }    
     if(key[KEY_1]&&!keyhold[KEY_1]){
         keyhold[KEY_1]=1;
         tpic--;
         if(tpic<0)tpic=45;
         map[ty][tx]=tpic;
         tiles[ty][tx].bmp=tpic;
     }    
     else {
          if(!key[KEY_1])keyhold[KEY_1]=0;
     }    
     if(key[KEY_2]&&!keyhold[KEY_2]){
         keyhold[KEY_2]=1;
         tpic++;
         if(tpic>45)tpic=0;
         map[ty][tx]=tpic;
         tiles[ty][tx].bmp=tpic;
     }
     else {
          if(!key[KEY_2])keyhold[KEY_2]=0;
     }
     if(key[KEY_3]&&!keyhold[KEY_3]){
         keyhold[KEY_3]=1;
          tdraw1--;tdraw2--;tdraw3--;tdraw4--;tdraw5--;
          if(tdraw1<0)tdraw1=45;
          if(tdraw2<0)tdraw2=45;
          if(tdraw3<0)tdraw3=45;
          if(tdraw4<0)tdraw4=45;
          if(tdraw5<0)tdraw5=45;
      }
     else {
          if(!key[KEY_3])keyhold[KEY_3]=0;
     }
     if(key[KEY_4]&&!keyhold[KEY_4]){
         keyhold[KEY_4]=1;
          tdraw1++;tdraw2++;tdraw3++;tdraw4++;tdraw5++;
          if(tdraw5>45)tdraw5=0;
          if(tdraw4>45)tdraw4=0;
          if(tdraw3>45)tdraw3=0;
          if(tdraw2>45)tdraw2=0;
          if(tdraw1>45)tdraw1=0;
      }     
     else {
          if(!key[KEY_4])keyhold[KEY_4]=0;
     }
     if(key[KEY_A]&&!keyhold[KEY_A]){
         keyhold[KEY_A]=1;
         tiles[ty][tx].att++;
         if(tiles[ty][tx].att>3)tiles[ty][tx].att=0;
     }               
     else {
          if(!key[KEY_A])keyhold[KEY_A]=0;
     }
     if(key[KEY_E]&&!keyhold[KEY_E]){
         keyhold[KEY_E]=1; 
         tiles[ty][tx].energy+=10;
         if(tiles[ty][tx].energy>=110)tiles[ty][tx].energy=0;  
     }               
     else {
          if(!key[KEY_E])keyhold[KEY_E]=0;
     }
     if(key[KEY_F]){
       for(i=0;i<MAP_H;i++){
        for(j=0;j<MAP_W;j++){
            tiles[i][j].bmp=tdraw3;
            tiles[i][j].att=0;      //Not Collidable by default Need to SET
            tiles[i][j].mov=0;      //Not Moveable by default
            tiles[i][j].energy=0;   //default not set either
        }
       }
       strcpy(cur_map_name,"NOT SAVED");
     }    
     if(key[KEY_D]){
          map[ty][tx]=tdraw3;
          tiles[ty][tx].bmp=tdraw3;
          tiles[ty][tx].att=1;
     }
     if(key[KEY_C]){
         tiles[ty][tx].att=0;
     }    
     if(key[KEY_L]&&!keyhold[KEY_L]){
         keyhold[KEY_L]=1;
         load_map(buffer);
     }    
     else {
          if(!key[KEY_L])keyhold[KEY_L]=0;
     }
     if(key[KEY_S]&&!keyhold[KEY_S]){
         keyhold[KEY_S]=1;     
         save_map(buffer);
     }    
     else {
          if(!key[KEY_S])keyhold[KEY_S]=0;
     }
     if(key[KEY_M]&&!keyhold[KEY_M]){
         keyhold[KEY_M]=1;
         tiles[ty][tx].mov++;
         if(tiles[ty][tx].mov>4)tiles[ty][tx].mov=0;
     }    
     else {
          if(!key[KEY_M])keyhold[KEY_M]=0;
     }
     /*if(key[KEY_DEL]){
         tdraw1=117;tdraw2=118;tdraw3=0;tdraw4=1;tdraw5=2;
         for(i=0;i<MAP_H;i++){
           for(j=0;j<MAP_W;j++){
              //clear_bitmap(tiles[i][j].bmp);
              tiles[i][j].att=0;      //Collidable by default
              tiles[i][j].mov=0;      //no movement
              tiles[i][j].energy=0;   //default
           }
         }
     }*/ // Do not use this, weird shit happens work it out later  
      
    if(key[KEY_RIGHT]){
       sx1+=TILE_SIZE;sx2+=TILE_SIZE;world_x+=TILE_SIZE;tx++;
       if(world_x+TILE_SIZE>map_x+EDIT_MAP_SIZE_W)map_x+=TILE_SIZE;
    }
    if(key[KEY_DOWN]){
       sy1+=TILE_SIZE;sy2+=TILE_SIZE;world_y+=TILE_SIZE;ty++;
       if(world_y+TILE_SIZE>map_y+EDIT_MAP_SIZE_H)map_y+=TILE_SIZE;
    }
    if(key[KEY_UP]){
       sy1-=TILE_SIZE;sy2-=TILE_SIZE;world_y-=TILE_SIZE;ty--;
       if(world_y<map_y)map_y-=TILE_SIZE;
    }
    if(key[KEY_LEFT]){
       sx1-=TILE_SIZE;sx2-=TILE_SIZE;world_x-=TILE_SIZE;tx--;
       if(world_x<map_x)map_x-=TILE_SIZE;
    }
}

void draw_tile_selector(void){
    int i,j;
    //rect(buffer,0,0,320,240,makecol(163,163,163));
    for(i=0;i<MAP_H;i++){
        for(j=0;j<MAP_W;j++){
            rect(buffer,j*16,i*16,(j+1)*16,(i+1)*16,makecol(0,0,63));
        }
    }       
    for(i=cam_y;i<cam_yoff;i++){
             for(j=cam_x;j<cam_xoff;j++){
                if(tiles[i][j].att)rect(buffer,tiles[i][j].x1,tiles[i][j].y1,tiles[i][j].x2,tiles[i][j].y2,makecol(0,255,0));  
             }
    } 
    rect(buffer,sx1,sy1,sx2,sy2,makecol(255,255,255)); 
}
    
#endif
